﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using WebUI.SmartDatingRef;

namespace WebUI {
    public class Utils {
        public static void showError(HtmlGenericControl span, string errorHtml) {
            span.InnerHtml = errorHtml;
            span.Style[HtmlTextWriterStyle.Display] = "block";
        }

        public static void hideError(HtmlGenericControl span) {
            span.Style[HtmlTextWriterStyle.Display] = "none";
        }

        public static string GetFromName(Object msg) {
            WebUI.SmartDatingRef.PersonalData pd = ((Msg) msg).MembershipFrom.PersonalData;
            return pd.first_name + " " + pd.last_name;
        }

        public static string GetToName(Object msg) {
            WebUI.SmartDatingRef.PersonalData pd = ((Msg)msg).MembershipTo.PersonalData;
            return pd.first_name + " " + pd.last_name;
        }

        public static string GetSubj(Object msg) {
            return ((Msg)msg).msg_subject;
        }

        public static string GetBody(Object msg) {
            return ((Msg)msg).msg_text.Replace("\n", "<br />");
        }

        public static string GetDateSent(Object msg) {
            DateTime dt = ((Msg)msg).date_sent;
            return dt.ToLongDateString() + " " + dt.ToLongTimeString();
        }

        public static string GetFromID(Object msg) {
            return ((Msg)msg).usr_from.ToString();
        }

        public static string GetToID(Object msg) {
            return ((Msg)msg).usr_to.ToString();
        }

        public static string GetMsgID(Object msg) {
            return ((Msg)msg).msg_id.ToString();
        }

        public static string GetShortBody(Object msg) {
            const int MAX_LENGTH = 150;
            const string ELLIPSIS = "…";
            char[] WRAPCHARS = { ' ', '.', '!', '?', ';', ':', '\n', '\t' };

            string body = ((Msg)msg).msg_text;
            string result;
            if (body.Length <= MAX_LENGTH) {
                result = body.Replace("\n", "<br />");
            } else {
                int wrapPos = 0;
                for (int i = MAX_LENGTH - 1; i >= 1; i--) {
                    if (WRAPCHARS.Contains(body[i])) {
                        wrapPos = i;
                        break;
                    }
                }
                if (wrapPos == 0) wrapPos = MAX_LENGTH - 1;

                result = body.Substring(0, wrapPos + 1) + ELLIPSIS;
            }
            return result.Replace("\n", "<br />");
        }

        public static string ReasonToString(int reason) {
            string[] REASONS = {"спонсор", "друг/подруга", "любовница/любовник", "жена/муж", "шофер, батут и клоун"};
            if (reason < 0 || reason > REASONS.Length) throw new ArgumentException("reason");
            return REASONS[reason];
        }

        public static string ZodiacToString(int zodiac) {
            if (zodiac == -1) return "";
            string[] SIGNS = {"водолей", "рыбы", "овен", "телец", "близнецы", "рак",
                  "лев", "дева", "весы", "скорпион", "стрелец", "козерог"};
            if (zodiac < 0 || zodiac > SIGNS.Length) throw new ArgumentException("zodiac");
            return SIGNS[zodiac];
        }

        public static string UsrId(object searchResult) {
            return ((SearchResult) searchResult).pd.usr_id.ToString();
        }

        public static string UsrFirstName(object searchResult) {
            return ((SearchResult)searchResult).pd.first_name;
        }

        public static string UsrLastName(object searchResult) {
            return ((SearchResult)searchResult).pd.last_name;
        }

        public static string UsrGender(object searchResult) {
            return ((SearchResult)searchResult).pd.gender.ToString();
        }

        public static bool UsrHasZodiac(object searchResult) {
            return ((SearchResult)searchResult).pd.zodiac.HasValue;
        }

        public static string UsrZodiac(object searchResult) {
            if (UsrHasZodiac(searchResult)) {
                return ((SearchResult)searchResult).pd.zodiac.ToString();
            } else {
                return "-1";
            }
        }

        public static string UsrBio(object searchResult) {
            return ((SearchResult)searchResult).pd.bio;
        }

        public static string UsrAge(object searchResult) {
            DateTime dateOfBirth = ((SearchResult)searchResult).pd.date_of_birth;
            return ((DateTime.UtcNow - dateOfBirth).Days / 365).ToString();
        }

        public static bool UsrHasPhone(PersonalData pd) {
            return (pd.phone_number != null && pd.phone_number.Length > 0);
        }

        public static bool UsrHasPhone(object searchResult) {
            return UsrHasPhone( ((SearchResult)searchResult).pd );
        }

        public static string UsrPhone(PersonalData pd) {
            if (UsrHasPhone(pd)) {
                return pd.phone_number;
            } else {
                return "";
            }
        }

        public static string UsrPhone(object searchResult) {
            return UsrPhone(((SearchResult)searchResult).pd);
        }

        public static bool UsrHasICQ(PersonalData pd) {
            return (pd.icq != null && pd.icq.Length > 0);
        }

        public static bool UsrHasICQ(object searchResult) {
            return UsrHasICQ(((SearchResult)searchResult).pd);
        }

        public static string UsrICQ(PersonalData pd) {
            if (UsrHasICQ(pd)) {
                return pd.icq;
            } else {
                return "";
            }
        }

        public static string UsrICQ(object searchResult) {
            return UsrICQ(((SearchResult)searchResult).pd);
        }

        public static bool UsrHasJabber(PersonalData pd) {
            return (pd.jabber != null && pd.jabber.Length > 0);
        }

        public static bool UsrHasJabber(object searchResult) {
            return UsrHasJabber(((SearchResult)searchResult).pd);
        }

        public static string UsrJabber(PersonalData pd) {
            if (UsrHasJabber(pd)) {
                return pd.jabber;
            } else {
                return "";
            }
        }

        public static string UsrJabber(object searchResult) {
            return UsrJabber(((SearchResult)searchResult).pd);
        }

        public static int UsrReason(object searchResult) {
            return ((SearchResult)searchResult).pd.reason;
        }

        public static bool UsrHasWeight(object searchResult) {
            return (((SearchResult)searchResult).pd.u_weight.HasValue);
        }

        public static string UsrWeight(object searchResult) {
            if (UsrHasWeight(searchResult)) {
                return ((SearchResult)searchResult).pd.u_weight.ToString();
            } else {
                return "-1";
            }
        }

        public static bool UsrHasHeight(object searchResult) {
            return (((SearchResult)searchResult).pd.u_height.HasValue);
        }

        public static string UsrHeight(object searchResult) {
            if (UsrHasHeight(searchResult)) {
                return ((SearchResult)searchResult).pd.u_height.ToString();
            } else {
                return "-1";
            }
        }

        public static bool UsrHasChest(object searchResult) {
            return (((SearchResult)searchResult).pd.chest.HasValue);
        }

        public static string UsrChest(object searchResult) {
            if (UsrHasChest(searchResult)) {
                return ((SearchResult)searchResult).pd.chest.ToString();
            } else {
                return "-1";
            }
        }


        public static bool UsrHasWaist(object searchResult) {
            return (((SearchResult)searchResult).pd.waist.HasValue);
        }

        public static string UsrWaist(object searchResult) {
            if (UsrHasWaist(searchResult)) {
                return ((SearchResult)searchResult).pd.waist.ToString();
            } else {
                return "-1";
            }
        }


        public static bool UsrHasHip(object searchResult) {
            return (((SearchResult)searchResult).pd.hip.HasValue);
        }

        public static string UsrHip(object searchResult) {
            if (UsrHasHip(searchResult)) {
                return ((SearchResult)searchResult).pd.hip.ToString();
            } else {
                return "-1";
            }
        }

        public static bool UsrHasProportions(object searchResult) {
            return (UsrHasWaist(searchResult) &&
                UsrHasChest(searchResult) && 
                UsrHasHip(searchResult));
        }

        public static string UsrProportions(object searchResult) {
            if (UsrHasProportions(searchResult)) {
                PersonalData pd = ((SearchResult)searchResult).pd;
                return pd.chest.ToString() + "-" + pd.waist.ToString() + "-" + pd.hip.ToString();
            } else {
                return "";
            }
        }

        public static bool UsrHasEyeColor(object searchResult) {
            return (((SearchResult)searchResult).pd.eye_color.HasValue);
        }

        public static string UsrEyeColor(object searchResult) {
            if (UsrHasEyeColor(searchResult)) {
                return ((SearchResult)searchResult).pd.eye_color.ToString();
            } else {
                return "-1";
            }
        }

        public static bool UsrHasHairColor(object searchResult) {
            return (((SearchResult)searchResult).pd.hair_color.HasValue);
        }

        public static string UsrHairColor(object searchResult) {
            if (UsrHasHairColor(searchResult)) {
                return ((SearchResult)searchResult).pd.hair_color.ToString();
            } else {
                return "-1";
            }
        }


        public static bool UsrHasMainSize(object searchResult) {
            return (((SearchResult)searchResult).pd.main_size.HasValue);
        }

        public static string UsrMainSize(object searchResult) {
            if (UsrHasMainSize(searchResult)) {
                return ((SearchResult)searchResult).pd.main_size.Value.ToString();
            } else {
                return "-1";
            }
        }

        public static string UsrInterests(object searchResult) {
            return ((SearchResult)searchResult).pd.interests;
        }

        public static string SizeToString(string s) {
            int size = int.Parse(s);
            if (size == -1) return "";

            string[] SIZES = { "экстра кинг сайз", "вэри биг", "медиум", "нормал", "маловато" };
            if (size < 0 || size > SIZES.Length) throw new ArgumentException("size");
            return SIZES[size];
        }

        public static string HairToString(string h) {
            int hair = int.Parse(h);
            if (hair == -1) return "";

            string[] HAIR = { "блондинко", "брюнетко", "каштановые", "светло-русые", "русые", "рыжие" };
            if (hair < 0 || hair > HAIR.Length) throw new ArgumentException("hair");
            return HAIR[hair];
        }

        public static string EyesToString(string e) {
            int eyes = int.Parse(e);
            if (eyes == -1) return "";

            string[] EYES = { "карие", "голубые", "зеленые", "серые", "серо-голубые", "красные" };
            if (eyes < 0 || eyes > EYES.Length) throw new ArgumentException("eyes");
            return EYES[eyes];
        }
    }
}